//
//  SKPhotographUpLoadViewController.m
//  Gastrosoph
//
//  Created by 张明磊 on 12-11-12.
//
//

#import "SKPhotographUpLoadViewController.h"
#import "SKHeaderFiles.h"
#import "SKCustomNavigationBar.h"
#import <MapKit/MapKit.h>



#define SK_BUTTON_RETURN_CGRECT CGRectMake(10, 4, 56, 36)

#define SK_LABEL_TITLE_CGRECT CGRectMake(125, 4, 100, 36)

#define SK_VIEW_SHARE_CGRECT CGRectMake(0, 44, 320, 416)

#define SK_VIEW_SEARCHRWATAURANTNAMEVIEW_CGRECT CGRectMake(0, 59, 320, 401)

#define SK_IMAGEVIEW_FOODBACKGROUND CGRectMake(35, 13, 250, 250)

#define SK_IMAGEVIEW_FOODPHOTO CGRectMake(39, 17, 242, 242)

#define SK_BUTTON_COMPLETE CGRectMake(8, 274, 303, 45)

#define SK_IMAGEVIEW_COMMENTLINE_RECT CGRectMake(0, 329, 320, 0.5)

#define SK_BUTTON_SHARE_RECT CGRectMake(14, 340, 292, 41)

#define SK_IMAGEVIEW_PROGRESSBACKGROUND CGRectMake(0, 44, 320, 15)

#define SK_SEARCHBAR_RESTAURANTNAME CGRectMake(0, 0, 320, 44)

#define SK_TABLEVIEW_SEARCHRESTAURANTNAME CGRectMake(0, 44, 320, 357)

//tableViewCell

#define SK_BUTTON_CELLBUTTON CGRectMake(10, 12, 33, 33)

#define SK_IMAGEVIEW_JIANTOU CGRectMake(300, 22, 10, 15)

#define SK_LABEL_ADD CGRectMake(54, 10, 200, 25)

#define SK_LABEL_RESTAURANT CGRectMake(54, 34, 200, 15)

#define SK_LABEL_NOSEARCH CGRectMake(80, 20, 150, 15)

#define SK_IMAGEVIEW_CELLCOMMENTLINE_RECT CGRectMake(0, 57, 320, 1)

//inputInformationForRestaurant

#define SK_MAPVIEW_RESTAURANTLOCARION_RECT CGRectMake(15, 15, 290, 154)

#define SK_IMAGEVIEW_INFORMATION_RECT CGRectMake(15, 185, 290, 115)

#define SK_BUTTON_SAVE_RECT CGRectMake(8, 330, 303, 45)



typedef enum
{
    SKButtonTag_return   = 0,
    SKButtonTag_complete    ,
    SKButtonTag_share       ,
    
}SKButtonTag;


@interface SKPhotographUpLoadViewController ()

@end

@implementation SKPhotographUpLoadViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
    }
    return self;
}

- (id)initWithImage:(UIImage *)_image
{
    self = [super init];
    
    if (self)
    {
        [self install_view_title];
        
        [self install_view_foodPhoto:_image];
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 
#pragma mark InstallView

- (void)install_view_title
{
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background.png"]]];
    
    SKCustomNavigationBar *customNavigationBar = [[SKCustomNavigationBar alloc] initWithFrame:SK_NAVIGATIONBAR_CGRECT];
    
    UIButton *button_return = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_return setFrame:SK_BUTTON_RETURN_CGRECT];
    
    [button_return setBackgroundImage:[UIImage imageNamed:@"button_foodDetail_return.png"] forState:UIControlStateNormal];
    
    [button_return setBackgroundImage:[UIImage imageNamed:@"button_foodDetail_return_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_return.titleLabel setFont:[UIFont fontWithName:SK_TEXTFONT size:18.0]];
    
    [button_return setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button_return setTitle:@"取消" forState:UIControlStateNormal];
    
    [button_return setTag:SKButtonTag_return];
    
    [button_return addTarget:self action:@selector(didClickButton_photoUpLoad:) forControlEvents:UIControlEventTouchUpInside];
    
    [customNavigationBar addSubview:button_return];
    
    label_titleLabel = [[UILabel alloc]initWithFrame:SK_LABEL_TITLE_CGRECT];
    
    [label_titleLabel setBackgroundColor:[UIColor clearColor]];
    
    [label_titleLabel setTextColor:[UIColor whiteColor]];
    
    [label_titleLabel setFont:[UIFont fontWithName:SK_TEXTFONT size:20.0]];
    
    [label_titleLabel setText:@"分享美食"];
    
    [customNavigationBar addSubview:label_titleLabel];
    
    [label_titleLabel release];
    
    [self.view addSubview:customNavigationBar];
    
    [customNavigationBar release];

}

- (void)install_view_foodPhoto:(UIImage *)_image
{
    view_viewChange = nil;
    
    view_viewChange = [[UIView alloc] initWithFrame:SK_VIEW_SHARE_CGRECT];
    
    [view_viewChange setBackgroundColor:[UIColor clearColor]];
    
    UIImageView *imageView_foodBackGround = [[UIImageView alloc]initWithFrame:SK_IMAGEVIEW_FOODBACKGROUND];
    
    [imageView_foodBackGround setImage:[UIImage imageNamed:@"imageView_photoUpLoad.png"]];
    
    [view_viewChange addSubview:imageView_foodBackGround];
    
    [imageView_foodBackGround release];
    
    UIImageView *imageView_foodPhoto = [[UIImageView alloc] initWithImage:_image];
    
    [imageView_foodPhoto setFrame:SK_IMAGEVIEW_FOODPHOTO];
    
    [view_viewChange addSubview:imageView_foodPhoto];
    
    [imageView_foodPhoto release];
    
    UIButton *button_complete = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_complete setFrame:SK_BUTTON_COMPLETE];
    
    [button_complete.titleLabel setFont:[UIFont systemFontOfSize:20.0f]];
    
    [button_complete setBackgroundImage:[UIImage imageNamed:@"button_complete.png"] forState:UIControlStateNormal];
    
    [button_complete setBackgroundImage:[UIImage imageNamed:@"button_complete_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_complete setTitle:@"完善“餐厅美食”信息" forState:UIControlStateNormal];
    
    [button_complete setTag:SKButtonTag_complete];
    
    [button_complete addTarget:self action:@selector(didClickButton_photoUpLoad:) forControlEvents:UIControlEventTouchUpInside];
    
    [view_viewChange addSubview:button_complete];
    
    UIImageView *image_commentLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageView_commentLine.png"]];
    
    [image_commentLine setFrame:SK_IMAGEVIEW_COMMENTLINE_RECT];
    
    [image_commentLine setAlpha:0.6f];
    
    [view_viewChange addSubview:image_commentLine];
    
    [image_commentLine release];
    
    UIButton *button_share = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_share setFrame:SK_BUTTON_SHARE_RECT];
    
    [button_share.titleLabel setFont:[UIFont systemFontOfSize:18.0f]];
    
    [button_share setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button_share setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    
    [button_share setTitle:@"  “自家烹饪美食”一键分享" forState:UIControlStateNormal];
    
    [button_share setTag:SKButtonTag_share];
    
    [button_share setBackgroundImage:[UIImage imageNamed:@"button_share.png"] forState:UIControlStateNormal];
    
    [button_share setBackgroundImage:[UIImage imageNamed:@"button_share_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_share addTarget:self action:@selector(didClickButton_photoUpLoad:) forControlEvents:UIControlEventTouchUpInside];
    
    [view_viewChange addSubview:button_share];
    
    [self.view addSubview:view_viewChange];
    
    [view_viewChange release];
}



#pragma mark -
#pragma mark DidClickButton
- (void)didClickButton_photoUpLoad:(id)sender
{
    UIButton *button_tmp = (UIButton *)sender;
    
    switch (button_tmp.tag)
    {
        case SKButtonTag_return:
        {
            [self dismissViewControllerAnimated:NO completion:nil];
            
        }break;
         
        case SKButtonTag_complete:
        {
            [self didViewChange_shareViewToAddView];
            
        }break;
            
        case SKButtonTag_share:
        {
            
        }break;
            
        default:
            break;
    }
}



#pragma mark - 
#pragma mark DidViewChange

- (void)didViewChange_shareViewToAddView
{
    [label_titleLabel setText:@"餐厅名"];
    
    [view_viewChange removeFromSuperview];
    
    UIImageView *imageView_progressOne = [[UIImageView alloc] initWithFrame:SK_IMAGEVIEW_PROGRESSBACKGROUND];
    
    [imageView_progressOne setImage:[UIImage imageNamed:@"imageView_progress1.png"]];
    
    [self.view addSubview:imageView_progressOne];
    
    [imageView_progressOne release];
    
    view_viewChange = nil;
    
    view_viewChange = [[UIView alloc] initWithFrame:SK_VIEW_SEARCHRWATAURANTNAMEVIEW_CGRECT];
    
    [view_viewChange setBackgroundColor:[UIColor clearColor]];
    
    UISearchBar *searchBar_restaurantName = [[UISearchBar alloc] initWithFrame:SK_SEARCHBAR_RESTAURANTNAME];
    
    [searchBar_restaurantName setPlaceholder:@"输入餐厅名"];
    
    [searchBar_restaurantName setShowsCancelButton:NO animated:NO];
    
    [searchBar_restaurantName setDelegate:self];
    
    [searchBar_restaurantName setTintColor:[UIColor lightGrayColor]];
    
    [view_viewChange addSubview:searchBar_restaurantName];
    
    [searchBar_restaurantName release];
    
    UITableView *tableView_searchRestaurantName = [[UITableView alloc] initWithFrame:SK_TABLEVIEW_SEARCHRESTAURANTNAME style:UITableViewStylePlain];
    
    [tableView_searchRestaurantName setDelegate:self];
    
    [tableView_searchRestaurantName setDataSource:self];
    
    [tableView_searchRestaurantName setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [tableView_searchRestaurantName setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background.png"]]];
    
    [view_viewChange addSubview:tableView_searchRestaurantName];
    
    [tableView_searchRestaurantName release];
    
    [self.view addSubview:view_viewChange];
    
    [view_viewChange release];
}

#pragma mark - 
#pragma mark UISearchBarDelegate

#pragma mark - 
#pragma mark UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        [cell.contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"view_shake_background.png"]]];
        
        UIImageView *image_commentLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageView_commentLine.png"]];
        
        [image_commentLine setFrame:SK_IMAGEVIEW_CELLCOMMENTLINE_RECT];
        
        [image_commentLine setAlpha:0.3f];
        
        [cell addSubview:image_commentLine];
        
        [image_commentLine release];
    }
    
    if(indexPath.row == 0)
    {
        UIButton *button_cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
        [button_cellButton setFrame:SK_BUTTON_CELLBUTTON];
    
        UIImageView *imageView_jiantou = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_imageView_rightArrow.png"]];
    
        [imageView_jiantou setFrame:SK_IMAGEVIEW_JIANTOU];
    
        [cell addSubview:imageView_jiantou];
    
        [imageView_jiantou release];
    
        [button_cellButton setBackgroundImage:[UIImage imageNamed:@"button_add.png"] forState:UIControlStateNormal];
    
        [button_cellButton setBackgroundImage:[UIImage  imageNamed:@"button_add_pressed.png"] forState:UIControlStateHighlighted];
        
        [button_cellButton addTarget:self action:@selector(didClickAddViewButton:) forControlEvents:UIControlEventTouchUpInside];
    
        [cell addSubview:button_cellButton];
    
        UILabel *label_add = [[UILabel alloc] initWithFrame:SK_LABEL_ADD];
    
        [label_add setBackgroundColor:[UIColor clearColor]];
    
        [label_add setFont:[UIFont systemFontOfSize:20.0f]];
    
        [label_add setText:@"添加一个新餐厅"];
        
        [cell addSubview:label_add];
    
        [label_add release];
    
        UILabel *label_restaurant = [[UILabel alloc] initWithFrame:SK_LABEL_RESTAURANT];
    
        [label_restaurant setBackgroundColor:[UIColor clearColor]];
    
        [label_restaurant setFont:[UIFont systemFontOfSize:15.0f]];
    
        [label_restaurant setTextColor:[UIColor brownColor]];
    
        [label_restaurant setText:@"天通苑"];
    
        [cell addSubview:label_restaurant];
        
        [label_restaurant release];

    }
    
    if(indexPath.row == 1)
    {
        UILabel *label_noSearch = [[UILabel alloc] initWithFrame:SK_LABEL_NOSEARCH];
        
        [label_noSearch setBackgroundColor:[UIColor clearColor]];
        
        [label_noSearch setTextColor:[UIColor grayColor]];
        
        [label_noSearch setFont:[UIFont systemFontOfSize:15.0f]];
        
        [label_noSearch setText:@"没有搜索到匹配的餐厅"];
        
        [cell addSubview:label_noSearch];
        
        [label_noSearch release];
    }
    
    return cell;
    
    [cell release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark -
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 58;
}


#pragma mark - 
#pragma mark DidClickCellButton
- (void)didClickAddViewButton:(id)sender
{
    [view_viewChange removeFromSuperview];
    
    view_viewChange = nil;
    
    view_viewChange = [[UIView alloc] initWithFrame:SK_VIEW_SEARCHRWATAURANTNAMEVIEW_CGRECT];
    
    [view_viewChange setBackgroundColor:[UIColor clearColor]];
    
    MKMapView *mapView_restaurant = [[MKMapView alloc] initWithFrame:SK_MAPVIEW_RESTAURANTLOCARION_RECT];
    
    [mapView_restaurant setTag:10];
    
    [mapView_restaurant setUserInteractionEnabled:NO];
    
    MKCoordinateSpan theSpan;
    
    theSpan.latitudeDelta = 0.02;
	
    theSpan.longitudeDelta = 0.02;
    
    MKCoordinateRegion theRegion;
    
    theRegion.center = [[[[[CLLocationManager alloc] init] autorelease] location] coordinate];
    
    theRegion.span = theSpan;
    
    [mapView_restaurant setRegion:theRegion];
    
    [mapView_restaurant showsUserLocation];
    
    [view_viewChange addSubview:mapView_restaurant];
    
    [mapView_restaurant release];
    
    UIImageView *imageView_information = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageView_informationBackground.png"]];
    
    [imageView_information setFrame:SK_IMAGEVIEW_INFORMATION_RECT];
    
    [view_viewChange addSubview:imageView_information];
    
    [imageView_information release];
    
    UITextView *textView_restaurantName = [[UITextView alloc] initWithFrame:CGRectMake(80, 185, 155, 37)];
    
    [textView_restaurantName setFont:[UIFont systemFontOfSize:18.0f]];
    
    [textView_restaurantName setTag:11];
    
    [textView_restaurantName setDelegate:self];
    
    [textView_restaurantName setTextColor:[UIColor grayColor]];
    
    [textView_restaurantName setBackgroundColor:[UIColor clearColor]];
    
    [view_viewChange addSubview:textView_restaurantName];
    
    [textView_restaurantName release];
    
    UITextView *textView_regional = [[UITextView alloc] initWithFrame:CGRectMake(80, 223, 155, 37)];
    
    [textView_regional setTag:12];
    
    [textView_regional setFont:[UIFont systemFontOfSize:18.0f]];
    
    [textView_regional setDelegate:self];
    
    [textView_regional setTextColor:[UIColor grayColor]];
    
    [textView_regional setBackgroundColor:[UIColor clearColor]];
    
    [view_viewChange addSubview:textView_regional];
    
    [textView_regional release];
    
    UITextView *textView_location = [[UITextView alloc] initWithFrame:CGRectMake(80, 260, 155, 37)];
    
    [textView_location setTag:13];
    
    [textView_location setFont:[UIFont systemFontOfSize:18.0f]];
    
    [textView_location setDelegate:self];
    
    [textView_location setTextColor:[UIColor grayColor]];
    
    [textView_location setBackgroundColor:[UIColor clearColor]];
    
    [view_viewChange addSubview:textView_location];
    
    [textView_location release];
    
    UIButton *button_save = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button_save setFrame:SK_BUTTON_SAVE_RECT];
    
    [button_save setBackgroundImage:[UIImage imageNamed:@"button_complete.png"] forState:UIControlStateNormal];
    
    [button_save setBackgroundImage:[UIImage imageNamed:@"button_complete_pressed.png"] forState:UIControlStateHighlighted];
    
    [button_save setTitle:@"保   存" forState:UIControlStateNormal];
    
    [view_viewChange addSubview:button_save];
    
    [self.view addSubview:view_viewChange];
    
    [view_viewChange release];
}


#pragma mark - 
#pragma mark UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{ 
    if(!tap_singleFingerTap){
        
        [UIView beginAnimations:nil context:nil];
    
        [UIView setAnimationDuration:0.5];
    
        [view_viewChange setBackgroundColor:[UIColor colorWithWhite:0.0f alpha:0.5]];
    
        [view_viewChange setFrame:CGRectMake(0, -110, view_viewChange.frame.size.width, view_viewChange.frame.size.height)];
    
        [UIView commitAnimations];
    
        tap_singleFingerTap =
    
        [[UITapGestureRecognizer alloc]initWithTarget:self
     
                                           action:@selector(didTouchView:)];
    
        [view_viewChange addGestureRecognizer:tap_singleFingerTap];
    
        [tap_singleFingerTap release];
    
        [self didSetMapViewHidden:YES];
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if(range.length != 1&& [text isEqualToString:@"\n"]){
    
        [UIView beginAnimations:nil context:nil];
        
        [UIView setAnimationDuration:0.25];
        
        [view_viewChange setBackgroundColor:[UIColor colorWithWhite:0.0f alpha:0.5]];
        
        [view_viewChange setFrame:SK_VIEW_SEARCHRWATAURANTNAMEVIEW_CGRECT];
        
        [UIView commitAnimations];
        
        [view_viewChange setBackgroundColor:[UIColor clearColor]];
        
        [self performSelector:@selector(didSetMapViewHidden:) withObject:NO afterDelay:0.25f];
        
        [view_viewChange removeGestureRecognizer:tap_singleFingerTap];
        
        tap_singleFingerTap = nil;
        
        [textView resignFirstResponder]; 
        
        return NO;
        
    }
    
    else{
        return YES;
    }
    
}

#pragma mark - 
#pragma mark UIViewTouchesBegan
- (void)didTouchView:(UITapGestureRecognizer *)recognizer
{
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationDuration:0.25];
    
    [view_viewChange setBackgroundColor:[UIColor colorWithWhite:0.0f alpha:0.5]];
    
    [view_viewChange setFrame:SK_VIEW_SEARCHRWATAURANTNAMEVIEW_CGRECT];
    
    UITextView *textView_name = (UITextView *)[view_viewChange viewWithTag:11];
    
    [textView_name resignFirstResponder];
    
    UITextView *textView_regional = (UITextView *)[view_viewChange viewWithTag:12];
    
    [textView_regional resignFirstResponder];
    
    UITextView *textView_location = (UITextView *)[view_viewChange viewWithTag:13];
    
    [textView_location resignFirstResponder];
    
    [UIView commitAnimations];
    
    [view_viewChange setBackgroundColor:[UIColor clearColor]];
    
     [self performSelector:@selector(didSetMapViewHidden:) withObject:NO afterDelay:0.25f];
     
    [view_viewChange removeGestureRecognizer:tap_singleFingerTap];
    
    tap_singleFingerTap = nil;

}

#pragma mark - 
#pragma mark didSetMapHidden

- (void)didSetMapViewHidden:(BOOL)_bool
{
    MKMapView *mapView_tmp = (MKMapView *)[view_viewChange viewWithTag:10];
    
    [mapView_tmp setHidden:_bool];
}


@end
